import QtQuick 1.1
import com.nokia.meego 1.0
import "qbconst.js" as Util
import "getUrls.js" as GetUrl
BorderImage {
    id: root

    property bool inMyCenter: false
    property bool inBrief
    signal forClicked
    signal againstClicked
    signal commentClicked
    signal contentClicked

    width: parent.width
    height: contentCol.height + 20
    source: "images/block_background.9.png"
    border { left: 10; right: 10; top: 10; bottom: 10 }

    MouseArea {
        anchors.fill: parent
        onClicked: contentClicked()
    }
    Item {
        id: contentCol
        height:children.height
        width: parent.width - 10
        Loader {
            id:loader1
            width: parent.width
            height:children.height
            anchors.top: parent.top

            visible: status === Loader.Ready
            sourceComponent: ( user === null || root.inMyCenter ) ? undefined : avatarRow
        }
        Loader {
            id:loader2
            width: parent.width
            height:children.height
            anchors.top: loader1.bottom
            visible: status === Loader.Ready
            sourceComponent: content ? contentComp : undefined
        }
        Loader {
            id:loader3
            width: parent.width
            height:children.height
            anchors.top: loader2.bottom

            visible: status === Loader.Ready
            sourceComponent: ( appWindow.showimage && (undefined!==image&&image !== null) ) ? imageComp : undefined
        }
        Loader {
            id:loader4
            width: parent.width
            height:children.height
            anchors.top: loader3.bottom
            visible: status === Loader.Ready
            sourceComponent: tag ? tagComp : undefined
        }
        Loader {
            id:loader5
            width: parent.width
            height:children.height
            anchors.top: loader4.bottom
            visible: status === Loader.Ready
            sourceComponent: (votes.down === null || votes.up === null ) ? undefined : voteComp
        }
        Loader {
            id:loader6
            width: parent.width
            height:children.height
            anchors.top: loader5.bottom
            visible: status === Loader.Ready
            sourceComponent: root.inMyCenter ? stateComp : undefined
        }
    }
    Component {
        id: avatarRow
        Row {
            spacing: 10
            Item {
                height: platformStyle.graphicSizeMedium;
                width: height
                Image {
                    id: avatarThumb
                    anchors.fill: parent
                    source: "images/thumb_avatar.png"
                }
                Image {
                    id: actuallAvatar
                    anchors.fill: parent
                    fillMode: Image.PreserveAspectFit
                    source: GetUrl.getavitorbyuser(user)
                    onStatusChanged: if (status===Image.Ready){
                                         avatarThumb.source = ""
                                         avatarThumb.visible = false
                                     }
                }
            }
            Label {
                width: parent.width-10
                color: appWindow.colorMidLight
                anchors.verticalCenter: parent.verticalCenter
                text: undefined===user?"匿名用户":user.login;
                elide: Text.ElideRight
            }
        }
    }
    Component {
        id: contentComp

        Text {

            font.pointSize: appWindow.fontsize
            color:appWindow.contntcolor
            clip:true
            width: parent.parent.width
            textFormat: Text.RichText
            text: content.replace(/\r/g,"")
            wrapMode: Text.Wrap
            elide: Text.ElideRight

            Image {
                visible: parent.truncated
                source: visible ? "images/icon_textmore.png" : ""
                anchors {
                    right: parent.right;
                    bottom: parent.bottom

                }
            }
        }
    }
    Component {
         id: imageComp


        Item {
            height: 163; width: 182
            Image {
                id: briefThumb
                anchors.fill: parent
                source: "images/thumb_pic.png"
            }
            Image {
                height: parent.height
                fillMode: Image.PreserveAspectFit
                source: GetUrl.getsmall(this,contentid, image)
                onStatusChanged: if (status===Image.Ready){
                                     parent.width = paintedWidth
                                     briefThumb.source = ""
                                     briefThumb.visible = false
                                 }
            }
            MouseArea {
                anchors.fill: parent
                onClicked: appWindow.pageStack.push(Qt.resolvedUrl("../ImagePage.qml"),
                                              {imageId: image})
            }
        }
    }

    Component {
        id: tagComp

        Row {
            Image {
                height: 24; width: 24
                anchors.verticalCenter: parent.verticalCenter
                source: "images/icon_tag.png"
            }
            Label {
                text: tag
                anchors.verticalCenter: parent.verticalCenter
                width: contentCol.width - 24

                platformStyle: LabelStyle{
                            textColor:appWindow.labelcolor
                            }
                wrapMode: Text.Wrap
            }
        }
    }

    Component {
        id: voteComp

        Row {

            VoteButton {
                type: "for"
                count: votes.up
                onClicked: root.forClicked()
            }
            VoteButton {

                type: "against"
                count: votes.down
                onClicked: root.againstClicked()
            }
            VoteButton {
                anchors.right: parent.right
                type: "comment"
                count: comments_count
                onClicked: root.commentClicked()
            }
        }
        }

    Component {

        id: stateComp

        Label {
            color: appWindow.colorMid
            text: {
                switch(model.state){
                case "pending": return "待审核"
                case "publish": return "审核已通过"
                case "private": return "未通过审核"
                default: return model.state
                }
            }
        }
    }
    }


